var TaskPanel = window?.chrome?.webview?.hostObjects?.TaskPanel || {
    JS_GetUserInfo: function () { return new Promise((resolve) => { resolve(); }); },
    JS_PushLastChat: function (prompt, content) { return new Promise((resolve) => { resolve(); }); },
};
window.callback = {};

TaskPanel.JS_GetUserInfo().then(userInfo => {
    if (!userInfo) return;
    user = JSON.parse(userInfo);
    setTimeout(() => {
        alert(`用户 ID: ${user.userId}, 注册 ID: ${user.regId}`);
    }, 1000);
});

window.callback.pushLastChat = function (message) {
    // const lastMessage = reactive({
    //     "id": assistantMessageId,
    //     "type": model,
    //     "subtype": "",
    //     "role": "assistant",
    //     "created": new Date().getTime(),
    //     "completed": false,
    //     "prompt": rawContent,
    //     "content": modelType === "image" ? "生成中..." : "",
    //     "sendLen": 0,
    //     "buffer": "",
    // });    
    TaskPanel.JS_PushLastChat(message.prompt, message.buffer);
};

async function ai_historys(){
    let h = await ai.getHistory(1);
    let s = JSON.stringify(h);
    console.log(s);
    return s;
}

function ai_historys3() {
    return ai.getHistory(1).then(history => {
        let h = JSON.stringify({
            flag: flag,
            data: history
        });
        return h;
    });
}

function ai_historys2(flag) {
    return ai.getHistory(1).then(history => {
        let h = JSON.stringify({
            flag: flag,
            data: history
        });
        window.chrome.webview.postMessage(h);
        return h;
    });
}

function ai_chat_messages() {
    return JSON.stringify(ai.chat.messages);
}